
package com.android.matmanual;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.TextView;




public class MagneticFieldTest extends Activity 
{
	
	TextView mMagneticFieldInfoX;
	TextView mMagneticFieldInfoY;
	TextView mMagneticFieldInfoZ;
	WakeLock mWakeLock;
	SensorManager mSensorManager;
	SensorEventListener mSensorEventListener;
	Sensor mSensor;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
     
		setContentView(R.layout.magnetic_field_test);
		setTitle(R.string.magnetic_field_test);
		PowerManager pm =(PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire(); 
		mMagneticFieldInfoX = (TextView) findViewById(R.id.magnetic_field_info_x);	
		mMagneticFieldInfoY = (TextView) findViewById(R.id.magnetic_field_info_y);
		mMagneticFieldInfoZ = (TextView) findViewById(R.id.magnetic_field_info_z);
		
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);  
        
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        mSensorEventListener = new SensorEventListener(){

			public void onAccuracyChanged(Sensor arg0, int arg1) {
				// TODO Auto-generated method stub


			}

			public void onSensorChanged(SensorEvent arg0) {
				// TODO Auto-generated method stub
				mMagneticFieldInfoX.setText(
						"X:"+arg0.values[SensorManager.DATA_X]);
				mMagneticFieldInfoY.setText(
						"Y:"+arg0.values[SensorManager.DATA_Y]);
				mMagneticFieldInfoZ.setText(
						"Z:"+arg0.values[SensorManager.DATA_Z]);


			}
          
            };
 		  	
        mSensorManager.registerListener(mSensorEventListener,mSensor, SensorManager.SENSOR_DELAY_UI);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}
	 @Override
	 protected void onDestroy() {
	    	// TODO Auto-generated method stub
			if(mWakeLock!=null)
			{
				mWakeLock.release(); 
				mWakeLock = null;
			}
			mSensorManager.unregisterListener(mSensorEventListener);
			
	    	super.onDestroy();
	}	
	 
	 
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}

	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

}
		 